package CloudStorageServlets;

import CloudStorage.*;
import CloudStorageExceptions.EmailExistsException;
import CloudStorageExceptions.UsernameExistsException;
import java.io.IOException;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class RegisterServlet extends HttpServlet
{
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException, UsernameExistsException, EmailExistsException, ClassNotFoundException, InstantiationException, IllegalAccessException
    {
        boolean register = true;
        HttpSession session = request.getSession();
        String surname = (String) request.getParameter("surname");
        String name = (String) request.getParameter("name");
        String email1 = (String) request.getParameter("email1");
        String email2 = (String) request.getParameter("email2");
        String username = (String) request.getParameter("username");
        String password1 = (String) request.getParameter("password1");
        String password2 = (String) request.getParameter("password2");

        if (surname.isEmpty() )
        {
            session.setAttribute("surname error message", "");
            register = false;
        }
        else
        {
            session.removeAttribute("surname error message");
        }

        if (name.isEmpty() )
        {
            session.setAttribute("name error message", "");
            register = false;
        } 
        else
        {
            session.removeAttribute("name error message");
        }

        if (email1.isEmpty() )
        {
            session.setAttribute("email1 error message", "");
            register = false;
        } 
        else
        {
            session.removeAttribute("email1 error message");
        }

        if (email2.isEmpty() )
        {
            session.setAttribute("email2 error message", "");
            register = false;
        }
        else
        {
            session.removeAttribute("email2 error message");
        }

        if ( email1 != null &&  email2 != null && email1.equals(email2) == false)
        {
            session.setAttribute("email not match error message", "");
            register = false;
        }
        else
        {
            session.removeAttribute("email not match error message");
        }

        if (username.isEmpty() )
        {
            session.setAttribute("username error message", "");
            register = false;
        } 
        else
        {
            session.removeAttribute("username error message");
        }

        if (password1.isEmpty() )
        {
            session.setAttribute("password1 error message", "empty");
            register = false;
        }
        else
        {
            session.removeAttribute("password1 error message");
        }

        if (password2.isEmpty() )
        {
            session.setAttribute("password2 error message", "");
            register = false;
        }
        else
        {
            session.removeAttribute("password2 error message");
        }

        if ( password1 != null && password2 != null && password1.equals(password2) == false)
        {
            session.setAttribute("password not match error message", "");
            register = false;
        }
        else
        {
            session.removeAttribute("password not match error message");
        }

        if ( register )
        {
            User newUser = new User(username, password1, email1, name, surname);
            Database database = new Database();
            session.setAttribute("registered", "");

            try
            {
                database.insertUser(newUser);
            }
            catch ( UsernameExistsException ex )
            {
                session.setAttribute("username exists", username);
                session.removeAttribute("registered");
            }
            catch ( EmailExistsException ex )
            {
                session.setAttribute("email exists", email1);
                session.removeAttribute("registered");
            }
            
            //Create user's directories
            session.setAttribute("user", newUser);
            session.setAttribute("status", "logged in");
            (new java.io.File(username)).mkdir();
        }


        response.sendRedirect("register.jsp");
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(RegisterServlet.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UsernameExistsException ex) {
            Logger.getLogger(RegisterServlet.class.getName()).log(Level.SEVERE, null, ex);
        } catch (EmailExistsException ex) {
            Logger.getLogger(RegisterServlet.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(RegisterServlet.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(RegisterServlet.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(RegisterServlet.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(RegisterServlet.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UsernameExistsException ex) {
            Logger.getLogger(RegisterServlet.class.getName()).log(Level.SEVERE, null, ex);
        } catch (EmailExistsException ex) {
            Logger.getLogger(RegisterServlet.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(RegisterServlet.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(RegisterServlet.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(RegisterServlet.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
